package com.congee02.runnable;

/**
 * @author congee02
 * @date 1/31/2023 5:26 PM
 */
public class ImplementRunnableTest {

    public static void main(String[] args) {
        ThreadProvider provider = new PrintRunnableImpl();
        provider.thread().start();
    }
    
    private static class PrintRunnableImpl implements Runnable, ThreadProvider {

        @Override
        public void run() {
            try {
                for (int i = 0 ; i < 100 ; i ++ ) {
                    Thread.sleep(1000);
                    System.out.println("Hola！ " + Thread.currentThread().getName() + " " + i);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
    }

    /**
     * 允许 获取 Runnable 被 Thread 包装的对象
     */
    private interface ThreadProvider {
        /**
         * 获取 Runnable 被 Thread 包装的对象
         *
         * @return Runnable 被 Thread 包装的对象
         */
        default Thread thread() {
            if (this instanceof Runnable) {
                return new Thread((Runnable) this);
            }
            return null;
        }
    }
    
}
